#include "GameOverState.h"

extern IDirectInputDevice8		*mJoystick[2]; // This will be changed to an area later...
extern DIJOYSTATE2				js;
//extern int prevJSButtons[12];
//extern int prevJSButtons2[12];
//extern int prevJS;
extern 	ID3DXSprite* mSprite;

GameOverState* GameOverState::init = 0;

GameOverState::GameOverState()
{
	HR(D3DXCreateTextureFromFile(gd3dDevice, "GameOver.png", &GameOverMenuTex));
	GameOverMenuCenter = D3DXVECTOR3(640.0f, 400.0f, 0.0f);
	HR(D3DXCreateTextureFromFile(gd3dDevice, "Player1.png", &Player1Tex));
	Player1Center = D3DXVECTOR3(128.0f, 32.0f, 0.0f);
	HR(D3DXCreateTextureFromFile(gd3dDevice, "Player2.png", &Player2Tex));
	Player2Center = D3DXVECTOR3(128.0f, 32.0f, 0.0f);
	
	//choice = NOONE;
	
	MenuViewPort.X	   = 0;
	MenuViewPort.Y	   = 0;
	MenuViewPort.Width   = 1280;
	MenuViewPort.Height  = 768;
	MenuViewPort.MinZ	   = 0.0f;
	MenuViewPort.MaxZ	   = 0.0f;

}

void GameOverState::Enter()
{

}

void GameOverState::Hate(PLAYERID num)
{
	choice = num;
}

void GameOverState::Execute(D3DApp* g)
{		
	static float cursorDelay = 0.0f;
	static float timeAccum = 0.0f;
	static int prevJS = 0;
	mJoystick[0]->GetDeviceState(sizeof(DIJOYSTATE2), &js);
		
	if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 0) && (js.rgdwPOV[0] ^ prevJS))
	{
		
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 9000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "2nd", "Error", 0);
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 18000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "3rd", "Error", 0);
	}

	else if(((js.rgdwPOV[0] != -1) && js.rgdwPOV[0] == 27000) && (js.rgdwPOV[0] ^ prevJS))
	{
		//MessageBox(gd3dApp->getMainWindowHandle(), "4th", "Error", 0);
	}

	else if((js.rgbButtons[0] & 0x80) && (prevJSButtons[0] ^ js.rgbButtons[0]))
	{
		//choice = 0;
	}

	else if(js.rgbButtons[1] & 0x80 && (prevJSButtons[1] ^ js.rgbButtons[1]))
	{
		//choice = 1;
	}

	else if(js.rgbButtons[2] & 0x80 && (prevJSButtons[2] ^ js.rgbButtons[2]))
	{
		//choice = '3';
	}

	else if(js.rgbButtons[3] & 0x80 && (prevJSButtons[3] ^ js.rgbButtons[3]))
	{
		//gd3dApp->setState(State_Menu::Instance());
	}

	else if(js.rgbButtons[4] & 0x80 && (prevJSButtons[4] ^ js.rgbButtons[4]))
	{
		PostQuitMessage(0);
	}

	if(!(js.rgdwPOV[0] == 4500 || js.rgdwPOV[0] == 13500 || js.rgdwPOV[0] == 22500 || js.rgdwPOV[0] == 31500))
			prevJS = js.rgdwPOV[0];

	for(int i = 0; i < 12; i++)
	{
		prevJSButtons[i] = js.rgbButtons[i];
	}

//	if(getPlayer1Health() == 1)
//	{
//		choice = '1';
//	}
//	else
//	{
//		choice = '2';
//	}

	Draw();

}

void GameOverState::Exit()
{
//	cout << "Exiting State_TwoPlayer\n";
}

void GameOverState::Draw()
{
	gd3dDevice->SetViewport( &MenuViewPort );
	// Now we can clear just view-port's portion of the buffer to red...
	HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0));

    HR(gd3dDevice->BeginScene());
    
    //draw stuff here
	HR(mSprite->Begin(D3DXSPRITE_OBJECTSPACE | D3DXSPRITE_DONOTMODIFY_RENDERSTATE));			

	D3DXMATRIX texScaling;
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	D3DXMATRIX T, S;
	D3DXMatrixTranslation(&T, -260.0f, 100.0f, 0.0f);
	D3DXMatrixScaling(&S, 0.68f, 1.0f, 0.0f);
	HR(mSprite->SetTransform(&(S*T)));

	////draw the menu
	HR(mSprite->Draw(GameOverMenuTex, 0, &GameOverMenuCenter, 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(mSprite->Flush());
	//restore the texture coordinate scaling transform. mirror texture 
	//coordinates with respect to y-axis so the texture is mapped correctly onto the sprite
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));


	if(choice == PLAYER1)
	{

	D3DXMATRIX texScaling;
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	D3DXMATRIX T, S;
	D3DXMatrixTranslation(&T, 0.0f, 0.0f, 0.0f);
	D3DXMatrixScaling(&S, 1.0f, 1.0f, 0.0f);
	HR(mSprite->SetTransform(&(S*T)));

	////draw the menu
	HR(mSprite->Draw(Player1Tex, 0, &Player1Center, 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(mSprite->Flush());
	//restore the texture coordinate scaling transform. mirror texture 
	//coordinates with respect to y-axis so the texture is mapped correctly onto the sprite
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	}
	if(choice == PLAYER2)
	{

	D3DXMATRIX texScaling;
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	D3DXMATRIX T, S;
	D3DXMatrixTranslation(&T, 0.0f, 0.0f, 0.0f);
	D3DXMatrixScaling(&S, 1.00f, 1.0f, 0.0f);
	HR(mSprite->SetTransform(&(S*T)));

	////draw the menu
	HR(mSprite->Draw(Player2Tex, 0, &Player2Center, 0, D3DCOLOR_XRGB(255, 255, 255)));
	HR(mSprite->Flush());
	//restore the texture coordinate scaling transform. mirror texture 
	//coordinates with respect to y-axis so the texture is mapped correctly onto the sprite
	D3DXMatrixScaling(&texScaling, 1.0f, -1.0f, 0.0f);
	HR(gd3dDevice->SetTransform(D3DTS_TEXTURE0, &texScaling));

	}

	//anything that needs to be drawn should be above this line and below the mSprite->Begin line
	HR(mSprite->End());

	HR(gd3dDevice->EndScene());

	//present the back buffer
	HR(gd3dDevice->Present(0, 0, 0, 0));
}

GameOverState* GameOverState::Instance()
{
	if(init == 0)
	{
		init = new GameOverState();

	}
	return init;
}
